/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 animation.cpp

	$Header: /resource_editor/animation.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "animation.h"

char const t_resource_traits<t_animation_24>::prefix[] = "animation";

char const t_resource_traits<t_animation_24>::extension[] = "psd";

// -------------------------------------------------------------------------
// 24 bit bitmap group with animation speed specified
// -------------------------------------------------------------------------
t_animation_24::t_animation_24()
{
	m_frame_delay = 100;
	m_x_is_valid = false;
	m_y_is_valid = false;
	m_base_point.x = 0;
	m_base_point.y = 0;
}

// -------------------------------------------------------------------------
// 24 bit bitmap group with animation speed specified
// -------------------------------------------------------------------------
static int const k_version = 2;

bool t_animation_24::read( std::streambuf& buffer, t_progress_handler* handler )
{
	if (!t_bitmap_group_24::read( buffer, handler ))
		return false;

	int version = get<t_uint16>( buffer );

	if (version > 10) // actually, version 0.
	{
		int i;

		version = 0;
		m_frame_delay = version;
		m_frame_order.resize( size() );
		for (i = 0; i < size(); i++)
			m_frame_order[i] = i;
	}
	else
	{
		m_frame_delay = get<t_uint16>(buffer);

		int count = get<int>( buffer );

		if (count == 0)
		{
			int i;

			m_frame_order.resize( size() );
			for (i = 0; i < size(); i++)
				m_frame_order[i] = i;	

		}
		else
		{
			m_frame_order.resize( count );
			::read( buffer, &(*m_frame_order.begin()), count * sizeof(int) );
		}
	}
	if (version < 2)
	{
		m_x_is_valid = false;
		m_y_is_valid = false;
		m_base_point.x = 0;
		m_base_point.y = 0;
	}	
	else
	{
		m_x_is_valid = get<bool>( buffer );
		m_y_is_valid = get<bool>( buffer );
		m_base_point = get<t_screen_point>( buffer );
	}
	return true;
}

// -------------------------------------------------------------------------
// 24 bit bitmap group with animation speed specified
// -------------------------------------------------------------------------
bool t_animation_24::write( std::streambuf& buffer ) const
{
	if (!t_bitmap_group_24::write( buffer ))
		return false;
	put<t_uint16>( buffer, k_version );
	put<t_uint16>( buffer, m_frame_delay );
	put<int>( buffer, m_frame_order.size() );
	::write( buffer, &(*m_frame_order.begin()), m_frame_order.size() * sizeof(int) );
	put<bool>( buffer, m_x_is_valid );
	put<bool>( buffer, m_y_is_valid );
	put<t_screen_point>( buffer, m_base_point );
	return true;
}

// -------------------------------------------------------------------------
// 16 bit bitmap group with animation speed specified
// -------------------------------------------------------------------------
t_animation::t_animation( t_animation_24 const& source ) 
           : t_bitmap_group( source ), t_animation_base( source )
{
}
